package com.xia.controller;

import cn.hutool.core.getter.ListTypeGetter;
import com.xia.dao.users.IUsersDao;
import com.xia.util.common.Result;
import com.xia.domian.Users;
import com.xia.service.IUserService;
import com.xia.util.exception.CodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.net.UnknownHostException;
import java.util.List;

/**
 * 用户控制器
 */
@CrossOrigin
@RestController
@RequestMapping("/users")
public class UsersController {
    private IUserService userService;
    private IUsersDao usersDao;
    @Autowired
    public void setUsersDao(IUsersDao usersDao) {
        this.usersDao = usersDao;
    }
    @Autowired
    public void setUserService(IUserService userService) {
        this.userService = userService;
    }

    /**
     * 查询用户个人信息
     */
    @GetMapping("/getUsersMessage/{usersId}")
    public Result getUsersMessage(@PathVariable Integer usersId){
        return Result.success(usersDao.selectUsersById(usersId));
    }

    /**
     * 用户登录
     * @param users 前端传入的登录消息
     */
    @PostMapping("/login")
    public Result login(@RequestBody Users users){
        Users usersData=userService.login(users);
        return Result.success("userData",usersData);
    }

    /**
     * 用户注册
     * @param users 前端传入的注册消息
     */
    @PostMapping("/register")
    public Result register(@RequestBody Users users){
        userService.registerUsers(users);
        return Result.success("注册成功");
    }

    /**
     * 用户修改信息
     * @param users 前端传入的修改消息
     */
    @PostMapping("/update")
    public Result update(@RequestBody Users users){
        if (usersDao.updateUsers(users) == 0){
            return Result.error("修改失败！");
        }
        return Result.success("修改成功，请重新登录~");
    }
}
